#导入模块
import datetime
import requests
import os
from bs4 import BeautifulSoup
import json
import pandas as pd
#因为这个项目是一个小时一个时间段的，而且有的时间段是空的，所以并不能直接按照电力系统的结构来写代码。
#首先爬虫这块就不能只是单独的，必须是得到他们给的数据的日期，来进行相关特征的添加，相当于这个数据处理包含了爬虫的内容
#是根据运行代码时候输入的内容，判断是历史数据预测，还是之后的数据预测，在本项目其实没有太大差别，每次运行都是获取以前所有数据？进行模型构造
iflastm = False

def path(input_path):
    curPath = os.path.abspath(os.path.dirname(__file__))
    rootPath = curPath[:curPath.find("aj\\") + len("aj\\")]
    dataPath = os.path.abspath(rootPath + input_path)
    return dataPath
def getdata(input_path=path('data/总电量.xlsx')):
    data = pd.read_excel(input_path)
    dict = {'时间': data['时间'],
            'ajdlbd.YC0053': data['ajdlbd.YC0053'],
            'ajdlbd.YC0865': data['ajdlbd.YC0865'],
            }
    dataframe = pd.DataFrame(dict)

    return dataframe

day = []
month = []
year = []
dataS = []
def dealYMD():
    dataframe=getdata()
    startyear = dataframe['时间'][0][:4]
    endyear = dataframe['时间'][len(dataframe['时间']) - 1][:4]

    # endyear=2022
    # print(startyear,endyear,type(startyear))

    # else:
    #     i = int(endyear) - int(startyear)
    #     for j in range(0, i + 1):
    #         year.append(int(startyear) + j)
    # print(year,type(year))

    startmonth = dataframe['时间'][0][5:7]
    endmonth = dataframe['时间'][len(dataframe['时间']) - 1][5:7]
    if (int(startyear) == int(endyear)):
        month.append(startmonth)
        # 目前只是2019年，先这么写
        for i in range(1, int(endmonth) - int(startmonth)):
            month.append(str(int(startmonth) + i))
        month.append(endmonth)
    else:
        i = int(endyear) - int(startyear)
        if (int(startmonth) == 12):
            month.append(startmonth)

        else:
            i = 12 - int(startmonth)
            for j in range(0, i + 1):
                month.append(str(int(startmonth) + j))
        if (int(endyear) - int(startyear) == 1):
            pass
        else:
            for j in range(0, int(endyear) - int(startyear) - 1):
                for num in range(1, 13):
                    month.append(str(num))

        if (int(endmonth) == 1):
            month.append(endmonth)
        else:

            for j in range(1, int(endmonth) + 1):
                month.append(str(j))
    # month=['06','7','8','09']
    # print(month,type(year))

    startdate = dataframe['时间'][0][8:10]
    enddate = dataframe['时间'][len(dataframe['时间']) - 1][8:10]
    day.append(startdate)
    for i in range(0, len(month) - 2):
        day.append('1')
    day.append(enddate)

    if (int(startyear) == int(endyear)):
        for i in range(0, int(endmonth) - int(startmonth) + 1):
            year.append(endyear)
    else:
        i = int(endyear) - int(startyear)
        if (int(startmonth) == 12):
            year.append(startyear)

        else:
            i = 12 - int(startmonth)
            for j in range(0, i + 1):
                year.append(startyear)
        if (int(endyear) - int(startyear) == 1):
            pass
        else:
            for j in range(0, int(endyear) - int(startyear) - 1):
                for num in range(1, 13):
                    year.append(str(int(startyear) + j + 1))

        if (int(endmonth) == 1):
            year.append(str(endyear))
        else:

            for j in range(1, int(endmonth) + 1):
                year.append(str(endyear))
    # print(month,len(month))
    # print(day,len(day))
    # print(year,len(year))

    for i in range(0, len(dataframe['时间'])):
        dataS.append(dataframe['时间'][i][0:10])
    return year,month,day,dataS



historycity_code=''
def judge_district(input_common_config='/data/common_config.csv',project_code="Default"):
    #z暂时project_code只有default
    if(project_code=='Default'):
        common_config_path=path(input_common_config)
        data=pd.read_csv(common_config_path)
        # history_city_code=data['history_city_code'][data['project_code']=='Default']
        # print(history_city_code)
        # return history_city_code
        for i in range(0,data.shape[0]):
            if(data['project_code'][i]=='default'):
                historycity_code=data['history_city_code'][i]

                # print(historycity_code)
        #

    return historycity_code
temp = []
templow = []
wind = []
humidity = []
desc = []
def history_crawlersameY(year,month,day,flag=len(temp)):
    dataframe = getdata()
    headers = {
        'User-Agent': '(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}




    historycity_code=judge_district()
    url = 'https://www.timeanddate.com/weather/'+historycity_code+'/historic?month=' + month + '&year=' + year
    # print(url)
    response = requests.get(url, headers=headers)

    soup = BeautifulSoup(response.text, 'html.parser')

    Data = soup.find_all('script', {'type': 'text/javascript'})

    datadetail = Data[3].get_text()

    s = ';window.month=' + str(month) + ';window.year=' + str(year) + ';'
    data1 = datadetail.replace(s, '')
    data2 = data1.replace('var data=', '')
    data2 = "".join([data2.strip().rsplit("}", 1)[0], "}"])
    date_json1 = json.loads(data2)

    detail = date_json1['detail']
    if(iflastm==True):
        i=0
        num = (int(day)) * 4
    else:
        num = len(detail)
        i = (int(day) - 1) * 4
    #这里还需要判断语句
    # if last:
    #     num=(int(day[0]) - 1) * 4
    #     i=0
    j = flag
    while j < len(dataS):
        try:

            ds = []
            if dataframe['时间'][j][6:7] == str(int(month) + 1):
                break
            if i == num:
                break
            if 'ds' in detail[i].keys():
                if detail[i]['ds'][18:19] == ':':
                    ds = detail[i]['ds'][16:18]
                else:
                    ds = detail[i]['ds'][17:19]

                while (int(dataframe['时间'][j][11:13]) < 6) and (int(ds) == 0):

                # 说明时间段相等
                #                 print(i,j,data['SDATE'][j])

                    if 'temp' in detail[i].keys():
                        temp.append(detail[i]['temp'])
                    else:
                        temp.append('未知')
                    if 'templow' in detail[i].keys():
                        templow.append(detail[i]['templow'])
                    else:
                        templow.append('未知')

                    if 'hum' in detail[i].keys():
                        humidity.append(detail[i]['hum'])
                    else:
                        humidity.append('未知')
                    if 'wind' in detail[i].keys():
                        wind.append(detail[i]['wind'])
                    else:
                        wind.append('未知')
                    if 'desc' in detail[i].keys():
                        desc.append(detail[i]['desc'])
                    else:
                        desc.append('未知')
                    j = j + 1

                while (6 <= int(dataframe['时间'][j][11:13]) < 12) and (int(ds) == 6):
                # 说明时间段相等

                #                 print(i,j,data['SDATE'][j])
                    if 'temp' in detail[i].keys():
                        temp.append(detail[i]['temp'])
                    else:
                        temp.append('未知')
                    if 'templow' in detail[i].keys():
                        templow.append(detail[i]['templow'])
                    else:
                        templow.append('未知')

                    if 'hum' in detail[i].keys():
                        humidity.append(detail[i]['hum'])
                    else:
                        humidity.append('未知')
                    if 'wind' in detail[i].keys():
                        wind.append(detail[i]['wind'])
                    else:
                        wind.append('未知')
                    if 'desc' in detail[i].keys():
                        desc.append(detail[i]['desc'])
                    else:
                        desc.append('未知')
                    j = j + 1
                while (12 <= int(dataframe['时间'][j][11:13]) < 18) and (int(ds) == 12):
                # 说明时间段相等
                #                 print(i,j)

                    if 'temp' in detail[i].keys():
                        temp.append(detail[i]['temp'])
                    else:
                        temp.append('未知')
                    if 'templow' in detail[i].keys():
                        templow.append(detail[i]['templow'])
                    else:
                        templow.append('未知')

                    if 'hum' in detail[i].keys():
                        humidity.append(detail[i]['hum'])
                    else:
                        humidity.append('未知')
                    if 'wind' in detail[i].keys():
                        wind.append(detail[i]['wind'])
                    else:
                        wind.append('未知')
                    if 'desc' in detail[i].keys():
                        desc.append(detail[i]['desc'])
                    else:
                        desc.append('未知')
                    j = j + 1

                while (18 <= int(dataframe['时间'][j][11:13]) <= 24) and (int(ds) == 18):
                # 说明时间段相等

                    if 'temp' in detail[i].keys():
                        temp.append(detail[i]['temp'])
                    else:
                        temp.append('未知')
                    if 'templow' in detail[i].keys():
                        templow.append(detail[i]['templow'])
                    else:
                        templow.append('未知')

                    if 'hum' in detail[i].keys():
                        humidity.append(detail[i]['hum'])
                    else:
                        humidity.append('未知')
                    if 'wind' in detail[i].keys():
                     wind.append(detail[i]['wind'])
                    else:
                     wind.append('未知')
                    if 'desc' in detail[i].keys():
                        desc.append(detail[i]['desc'])
                    else:
                        desc.append('未知')
                    j = j + 1
                i = i + 1
        except Exception:
            pass


    flag=len(temp)
    dict = {
            'temphigh': temp,
            'templow': templow,

            'wind': wind,
            'humidity': humidity,
            'describe': desc,
            }
    dataframe2 = pd.DataFrame(dict)
    # print(dataframe2,j)
    # print(len(temp),iflastm,num,day)
    return temp,templow,humidity,wind,desc,dataframe2,flag

is_holidaylist = []
def is_Holiday():
    start_date = dataS[0]
    end_date = dataS[len(dataS) - 1]
    # set_date = datetime.datetime.strptime(start_date,"%Y-%m-%d")

    is_mondaylist = []
    date = []
    '''判断当天日期是否为节假日'''
    rest_holiday=pd.read_csv(path('data/rest_holiday.csv'))
    rest_workday = pd.read_csv(path('data/rest_workday.csv'))

    # 把调休的休息日加到这里面
    # rest_holiday = [
    #     '2018-12-31',
    #     '2019-01-01', '2019-02-04', '2019-02-05', '2019-02-06', '2019-02-07', '2019-02-08',
    #     '2019-04-05', '2019-04-29', '2019-04-30', '2019-05-01', '2019-06-07', '2019-09-13',
    #     '2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04', '2019-10-07', '2019-12-30',
    #     '2019-12-31',
    #
    # ]
    # # 把调休的工作日加到这里面
    # rest_workday = [
    #     '2019-02-02', '2019-02-03', '2019-04-27', '2019-02-28', '2019-09-29', '2019-10-12',
    #     '2019-12-28', '2019-12-29',
    #
    # ]
    for i in range(0, len(dataS)):
        set_date = datetime.datetime.strptime(dataS[i], "%Y-%m-%d")
        set_date_str = set_date.strftime('%Y-%m-%d')
        #         if set_date_str>end_date:
        #             break
        # 0~6代表周一~周日
        #         set_date_str=dataS[i]
        weekday = set_date.weekday()
        if set_date_str in rest_holiday or (weekday in [5, 6] and set_date_str not in rest_workday):
            is_holiday = 1
            is_monday = 0
        else:
            is_holiday = 0
            is_monday = 1
        '''
        #这里的sql语句可根据自己的需要进行调整
        sql="INSERT INTO dmdc.t_is_holiday(`date`,is_holiday,is_monday) VALUES ('%s',%s,%s);"%(set_date_str,is_holiday,is_monday)
        #把sql语句写入sql文件
        with open('./date_is_holiday.sql','a+') as f:
            f.write(sql+'\n')

         '''

        is_holidaylist.append(is_holiday)
        is_mondaylist.append(is_monday)
    return is_holidaylist
if __name__ == '__main__':
    # # getenergy()
    dataframe=getdata()
    dealYMD()
    for i in range(0,len(month)):
        if(i==len(month)-1):
            iflastm=True
    # for month in month:
    #     history_crawlersameY(year=2019, month=month, day='1', flag=len(temp))
        history_crawlersameY(year=year[i], month=month[i], day=day[i], flag=len(temp))
    is_Holiday()
    dict = {'时间': dataframe['时间'],
            'ajdlbd.YC0053': dataframe['ajdlbd.YC0053'],
            'ajdlbd.YC0865': dataframe['ajdlbd.YC0865'],
            'temphigh': temp,
            'templow': templow,
            'is_holiday': is_holidaylist,
            'wind': wind,
            'humidity': humidity,

            }
    dataframe = pd.DataFrame(dict)
    print(dataframe)
    # dataframe.to_csv('D:\\总电量test.csv', sep=',', header=None, index=None,encoding="utf_8_sig")#改成覆盖
    dataframe.to_csv(path('data/总电量test.csv'), sep=',', index=None, encoding="utf_8_sig")  # 改成覆盖
    # print(path('总电量.xlsx'))
    # if os.path.exists("D:/总电量test.csv"):
    #     dataframe.to_csv('D:\\总电量test.csv', mode='a', sep=',', header=None, index=None,
    #                      encoding="utf_8_sig")
    # else:
    #     dataframe.to_csv('D:\\总电量test.csv', mode='a', sep=',', index=None, encoding="utf_8_sig")

